package cn.flashk {
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import cn.flashk.controls.List;
	import cn.flashk.controls.managers.DefaultStyle;
	import cn.flashk.controls.managers.StyleManager;
	import cn.flashk.controls.managers.UISet;
	import cn.flashk.controls.support.BaseListItemRender;
	import cn.flashk.controls.support.ColorConversion;
	
	/**
	 * 列表项自定义实例,默认大小为100,25
	 * @author yu
	 * 
	 */	
	public class CustomItem extends BaseListItemRender {
		private var txt:TextField;
		private var tf:TextFormat;
		private var bg:Shape;
		private var _index:int;
		private var _mouseOutAlpha:Number;
		
		public function CustomItem() {
			txt = new TextField();
			_height = List.defaultItemHeight;
			txt.mouseEnabled = false;
			this.mouseChildren = false;
			txt.y = 2;
			txt.x = 8;
			txt.y = int((_height - 19) / 2);
			txt.y = txt.y + DefaultStyle.fontExcursion;
			tf = new TextFormat();
			tf.font = DefaultStyle.font;
			tf.color = ColorConversion.transformWebColor(DefaultStyle.checkBoxTextOverColor);
			txt.defaultTextFormat = tf;
			this.addChild(txt);
			txt.height = _height - 2;
			
			bg = new Shape();
			this.addChildAt(bg, 0);
			this.addEventListener(MouseEvent.MOUSE_OUT,showOut)
			this.addEventListener(MouseEvent.MOUSE_OVER,showOver)
		}
		
		/**
		 * 设置数据 
		 * @param value
		 */		
		override public function set data(value:Object):void {
			super.data = value;
			txt.text = value.label;
			txt.width = txt.textWidth + 5;
			txt.cacheAsBitmap = UISet.listItemTextFieldCatch;
			this.cacheAsBitmap = UISet.listRenderCath;
		}
		
		override public function set index(value:int):void {
			super.index = value;
			_index = value;
			if (value % 2 == 0){	//AS皮肤隔行显示
				_mouseOutAlpha = StyleManager.listIndex1Alpha;
			} else {
				_mouseOutAlpha = StyleManager.listIndex2Alpha;
			}
			if (_selected == false){
				if (bg)
					bg.alpha = _mouseOutAlpha;
			}
		}
		
		override public function setSize(newWidth:Number, newHeight:Number):void {
			super.setSize(newWidth,newHeight);
			bg.graphics.clear();
			bg.graphics.beginFill(0xCCCCCC,0);
			bg.graphics.drawRect(1, 0, _width - 1, _height);
			bg.graphics.endFill();
		}
		
		override public function showSelect(event:MouseEvent=null):void {
			bg.alpha = StyleManager.listClickAlpha;
		}
		
		override protected function showOut(event:MouseEvent=null):void {
			if(!_selected){
				bg.alpha = _mouseOutAlpha;
			}
		}
		
		override protected function showOver(event:MouseEvent=null):void {
			bg.alpha = StyleManager.listOverAlpha;
		}
		
		override public function destory():void {
			super.destory();
			txt = null;
			tf  = null;
			bg  = null;
		}
	}
}